import API from '../../utils/api'
const app = getApp()

Page({
  data: {
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 10000,
    duration: 2000,
    activityList: [],
    hotList: [],
    hotListceshi: [],
    isTransform: false,
    isTransformTwo: false,
    isTransformThree: false,
    switchNumOne: 0,
    switchNumTwo: 0,
    switchNumThree: 0,
    weekActive: [],
    recommendList: [],
    hotList: [],
    hotListCirculation: [],
    recommendListCirculation: [],
    weekCirculation: [],
    page: 1,
    noData: false,
    cityName: '',
    isLoading: false
  },

  onLoad: function () {



  },
  onShow: function () {
    if (wx.getStorageSync('editproduct_id')) {
      console.log('是否清楚缓存')
      wx.removeStorageSync('setMoney')
      wx.removeStorageSync('pcate_id')
      wx.removeStorageSync('cate_id')
      wx.removeStorageSync('activityDetailPic')
      wx.removeStorageSync('product_id')
      wx.removeStorageSync('address')
      wx.removeStorageSync('latitude')
      wx.removeStorageSync('longitude')
      wx.removeStorageSync('pcate_name')
      wx.removeStorageSync('editproduct_id')
      wx.removeStorageSync('UpPic')
      wx.removeStorageSync('store_name')
      wx.removeStorageSync('pcate_name')
      wx.removeStorageSync('phone')
    }
    
    console.log('首页')
    if (!wx.getStorageSync("userInfo")) {
      wx.navigateTo({
        url: '/pages/login/login?form=1'
      })
    } else {
      this.getIndexData()
      
    }
    this.setData({
      hotListceshi: []
    })

    var cityName = wx.getStorageSync('cityName')
    if (!cityName) {
      this.getLocation()
    } else {
      this.setData({
        cityName: wx.getStorageSync('cityName')
      })
    }

  },
  // 获取当前位置经维度
  getLocation() {
    var _this = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        _this.mapInfo(longitude, latitude)
      }
    })
  },
  mapInfo(longitude, latitude) {
    var _this = this
    console.log(location)
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=VT6BZ-C5LKP-5KLDX-VJFAK-7S326-AMBQO',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log('获取城市')
        console.log(res.data.result.address_component.city)
        _this.getCity(res.data.result.address_component.city)

      }
    })
  },
  // 获取城市数据
  getCity(cityName) {
    console.log(cityName)
    console.log('是否到这里了')
    API.cityList().then(res => {
      console.log(res)
      if (res.code == 0) {
        var city = res.Data.allcity

        var cityList = []
        for (var k in city) {
          for (var j in city[k].city) {
            if (city[k].city[j].childname.substr(0, 2) == cityName.substr(0, 2)) {
              wx.setStorageSync('cityName', city[k].city[j].childname)
              wx.setStorageSync('nowCity', city[k].city[j].childname)
              wx.setStorageSync('childid', city[k].city[j].childid)
              return
            }
          }
        }
      }
    })
  },
  getIndexData() {
    var data = {
      areaid: wx.getStorageSync("childid")
    }
    API.indexData(data).then(res => {
      if (res.code == 0) {
        var newWeek = []
        var hotList = []
        var recommendList = []
        var week = res.data.weekActive
        var hot = res.data.HotActive
        var recommend = res.data.goodActive
        wx.setStorageSync('week', week)
        wx.setStorageSync('recommend', recommend)
        for (var k = 0; k < week.length; k += 3) {
          newWeek.push(week.slice(k, k + 4))
        }
        for (var j = 0; j < hot.length; j += 3) {
          hotList.push(hot.slice(j, j + 4))
        }
        for (var i = 0; i < recommend.length; i += 3) {
          recommendList.push(recommend.slice(i, i + 4))
        }

        var recom = [] // 推荐
        var hot = []
        var week = []
        if (recommendList.length > 0) {
          recom = recommendList[0]
        }
        if (hotList.length > 0) {
          hot = hotList[0]
        }
        if (newWeek.length > 0) {
          week = newWeek[0]
        }
        this.setData({
          imgUrls: res.data.Banner,
          weekActive: newWeek,
          weekCirculation: week,
          recommendList: recommendList,
          hotList: hotList,
          recommendListCirculation: recom,
          hotListCirculation: hot
        })
        this.pullRequestData(1)
      }
    })
  },
  // 周末活动换一换
  switchover() {
    var num = this.data.switchNumOne + 1
    if (num > this.data.weekActive.length - 1) {
      num = 0
    }
    var result = this.data.weekActive[num]
    this.setData({
      isTransform: true,
      weekCirculation: result,
      switchNumOne: num
    })
    setTimeout(() => {
      this.setData({
        isTransform: false
      })
    }, 500)
  },
  // 推荐活动换一换
  switchoverRecommend() {
    var num = this.data.switchNumTwo + 1
    if (num > this.data.recommendList.length - 1) {
      num = 0
    }
    var result = this.data.recommendList[num]
    this.setData({
      isTransformTwo: true,
      recommendListCirculation: result,
      switchNumTwo: num
    })
    setTimeout(() => {
      this.setData({
        isTransformTwo: false
      })
    }, 500)
  },
  // 热门活动换一换
  switchoverHot() {
    var num = this.data.switchNumThree + 1
    if (num > this.data.hotList.length - 1) {
      num = 0
    }
    var result = this.data.hotList[num]
    this.setData({
      isTransformThree: true,
      hotListCirculation: result,
      switchNumThree: num
    })
    setTimeout(() => {
      this.setData({
        isTransformThree: false
      })
    }, 500)
  },
  // 去活动详情页面
  toActivityDetail(e) {
    var id = e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: '/pages/activityDetail/activityDetail?id=' + id
    })

  },
  // 上新
  pullRequestData(page, ispull) {
    var data = {
      page: page,
      areaid: wx.getStorageSync('childid'),
      pageSize: 10
    }
    API.pullRequest(data).then(res => {
      if (res.code == 0) {
        var deal = []
        if (res.data.length > 0) {
          console.log(page)
          deal = this.data.hotListceshi.concat(res.data)
          this.setData({
            hotListceshi: deal
          })
        } else {
          if (ispull != 1) {
            this.setData({
              noData: true,
              hotListceshi: []
            })
          }

        }
        this.setData({
          isLoading: false
        })
        console.log(this.data.hotListceshi)
      }
    })
  },
  onReachBottom: function () {

    var page = this.data.page
    page++
    this.setData({
      page: page,
      isLoading: true
    })
    this.pullRequestData(page, 1)

  },
  onPullDownRefresh: function () {
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 300)

  }
})